/**
 * @(#) ReqWorkTimeProxy.as
 *
 * @date		2009. 03. 10.
 * @version		1.0
 * @author		Shin, Yunwoo
 * @description	 근태신청 Proxy - Proxy
 * 				근태신청 리스트를 조회하고 저장, 수정, 삭제한다.
 */
package com.skt.common_hr.work.model
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.work.WorkFacade;
	import com.skt.common_hr.work.model.vo.ReqVacationListVO;
	import com.wcl.dataloader.*;
	
	import mx.collections.ArrayCollection;
	import mx.managers.CursorManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
		
	public class ReqWorkTimeProxy extends CommonProxy
	{
		public static const NAME:String = "ReqWorkTimeProxy";
		
		public var reqVacationList:ArrayCollection;
		public var outKey:String;
		
		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function ReqWorkTimeProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번
		 * @return void
		 */
		public function InsertWorkTime(type:String, table:Array, html:String=""):void
		{
			var param:Object = new Object();
			//param["I_PROC_TYPE"] 	= "I";				//처리구분(I:신규,M:수정)
			//param["I_WSTATUS"]		= type;				//결재유형(1:임시저장,2:승인요청)
			//param["I_HTML"]			= html; //buildHTML(table);
			//param["I_XML"]			= buildXML();
			//trace(param["I_XML"]);
			
			var tables:Object = new Object();
			tables["ITAB"] = table;
			
			saveData("Z_HR_0022", param, tables, resultInsert, faultInsert);
		}



		
		public function deleteWorkTime(key:Array):void
		{
			var param:Object = new Object();
			var tables:Object = new Object();
			tables["ITAB"] = key;
			trace("delete worktimes: " + key.length);
			saveData("Z_HR_0069", param, tables, resultDelete, faultDelete); 
		}
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function resultLoad(event:ResultEvent):void
		{
			if(retCode==0) {  
				reqVacationList = parseVacationList(event.result.IOTAB);
				facade.sendNotification(WorkFacade.LOAD_REQ_VACATION_COMPLETE, this);
			}
			else {
				facade.sendNotification(WorkFacade.LOAD_REQ_VACATION_FAILED, this);
			}
		}
		
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function faultLoad(event:FaultEvent):void
		{
			facade.sendNotification(WorkFacade.LOAD_REQ_VACATION_FAILED, this);
		}
		
		
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function resultInsert(event:ResultEvent):void
		{
			if(retCode==0) {  
				outKey = event.result["O_GWAREKEY"];
				trace("저장성공: " + outKey);
				facade.sendNotification(WorkFacade.INSERT_REQ_WORKTIME_COMPLETE, this);
			}
			else {
				facade.sendNotification(WorkFacade.INSERT_REQ_WORKTIME_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function faultInsert(event:FaultEvent):void
		{
			facade.sendNotification(WorkFacade.INSERT_REQ_WORKTIME_FAILED, this);
		}
		
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function resultDelete(event:ResultEvent):void
		{
			if(retCode==0) {  
				facade.sendNotification(WorkFacade.DELETE_REQ_VACATION_COMPLETE, this);
			}
			else {
				facade.sendNotification(WorkFacade.DELETE_REQ_VACATION_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function faultDelete(event:FaultEvent):void
		{
			facade.sendNotification(WorkFacade.DELETE_REQ_VACATION_FAILED, this);
		}
		
		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 제증명서 종류 Code list
		 */
		private function parseVacationList(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			
			for each(var rec:Object in srcArr) 
			{
				var vo:ReqVacationListVO = new ReqVacationListVO();
				vo.PERNR 		= rec["PERNR"];
				vo.ENAME		= rec["ENAME"];
				vo.DGBN			= rec["DGBN"];
				vo.DGBNT		= rec["DGBNT"];
				vo.AWART		= rec["AWART"];
				vo.AWATX		= rec["AWATX"];
				vo.BEGDA		= rec["BEGDA"];
				vo.ENDDA		= rec["ENDDA"];
				vo.BEGUZ		= rec["BEGUZ"];
				vo.ENDUZ		= rec["ENDUZ"];
				vo.TIMES		= rec["TIMES"];
				vo.IPERNR		= rec["IPERNR"];
				vo.INAME		= rec["INAME"];
				vo.WPERNR		= rec["WPERNR"];
				vo.WSTATUS		= rec["WSTATUS"];
				vo.WSTATX		= rec["WSTATX"];
				vo.TABNR		= rec["TABNR"];
				vo.TEXT			= rec["TEXT1"];
				retArr.addItem(vo);
			}
			return retArr;	
		}
		
		/*
		private function buildHTML(table:Array):String
		{
			var htmlAll:String = templateHTML;
			var htmlBlock:String = "";
			//<TD class=rowlabel width=80 height=30><P>근태유형</P></TD>
			//<TD class=rowlabel width=90 height=30><P>근태일자</P></TD>
			//<TD class=rowlabel width=50 height=30><P>시작시간</P></TD>
			//<TD class=rowlabel width=50 height=30><P>종료시간</P></TD>
			//<TD class=rowlabel width=270 height=30><P>내용</P></TD>
			for each(var vo:ReqVacationVO in table)
			{
				var htmlLine:String = "<TR>\n"
				htmlLine += "<TD style='background: #fdfdf3; text-align: center; font-size: 9pt; border-bottom-color : #000000; border-bottom-style : solid; border-bottom-width : 1px; border-right-color : #000000; border-right-style : solid;    border-right-width : 1px;' width=80 height=20>&nbsp;" + StringUtil.NVL(vo.AWATX) + "</TD>\n";
				htmlLine += "<TD style='background: #fdfdf3; text-align: center; font-size: 9pt; border-bottom-color : #000000; border-bottom-style : solid; border-bottom-width : 1px; border-right-color : #000000; border-right-style : solid;    border-right-width : 1px;' width=90 height=20>&nbsp;" + DateUtil.toDefaultString(vo.BEGDA) + "</TD>\n";
				htmlLine += "<TD style='background: #fdfdf3; text-align: center; font-size: 9pt; border-bottom-color : #000000; border-bottom-style : solid; border-bottom-width : 1px; border-right-color : #000000; border-right-style : solid;    border-right-width : 1px;' width=50 height=20>&nbsp;" + DateUtil.toTimeString(vo.BEGUZ) + "</TD>\n";
				htmlLine += "<TD style='background: #fdfdf3; text-align: center; font-size: 9pt; border-bottom-color : #000000; border-bottom-style : solid; border-bottom-width : 1px; border-right-color : #000000; border-right-style : solid;    border-right-width : 1px;' width=50 height=20>&nbsp;" + DateUtil.toTimeString(vo.ENDUZ) + "</TD>\n";
				htmlLine += "<TD style='background: #fdfdf3; text-align: left; font-size: 9pt; border-bottom-color : #000000; border-bottom-style : solid; border-bottom-width : 1px; border-right-color : #000000; border-right-style : solid;    border-right-width : 1px;' width=270 height=20>&nbsp;" + StringUtil.NVL(vo.TEXT1) + "</TD></TR>\n";
				htmlBlock += htmlLine;
			}
			
			var ptnBlock:RegExp = /__RECORD_BLOCK__/; 
			var ptnMember:RegExp = /__MEMBER__/;
			htmlAll = htmlAll.replace(ptnMember, Variables.myName);
			return htmlAll.replace(ptnBlock, htmlBlock);
		}

		private function buildXML():String
		{
			var tmpXML:XML = new XML("<header>"+
						"<line>"+
							"<line_author></line_author>"+
							"<line_reviewer seq='1'></line_reviewer>"+
							"<line_reviewer seq='2'></line_reviewer>"+
							"<line_approver></line_approver>"+
							"<line_docharger></line_docharger>"+
						"</line>"+
						"<inf>"+
						    "<inf_type>HR</inf_type>"+
						    "<inf_formkind>IF_HR_002</inf_formkind>"+
						    "<inf_keycode>__keycode__</inf_keycode>"+
						    "<inf_subject>근태 품의</inf_subject>"+
						    "<inf_createdt>작성일자 : yyyy-mm-dd mm:hh:ss</inf_createdt>"+
						    "<inf_htmlnm>__filename__</inf_htmlnm>"+
						"</inf>"+
					"</header>");
			tmpXML.line.line_author = StringUtil.trimFront(Variables.myPernr, "0");
			tmpXML.line.line_approver = Variables.myBossCode;
			//tmpXML.line.line_docharger = Variables.getModuleManager("WORK");
			tmpXML.inf.inf_createdt = DateUtil.today();
			return "<?xml version='1.0' encoding='utf-8'?>" + tmpXML.toXMLString();
		}
		*/
	}
}